package leetcode.code1508;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	int mod = (int) 1e9 + 7;

	public int rangeSum(int[] nums, int n, int left, int right) {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < nums.length; i++) {
			for (int j = i, sum = 0; j >= 0; j--) {
				sum += nums[j];
				list.add(sum);
			}
		}
		Collections.sort(list);
		long ans = 0;
		for (int i = left - 1; i < right; i++) {
			ans += list.get(i);
			ans %= mod;
		}
		return (int) ans;
	}

	@Override
	public void debug4() {
		H.compare(13, this.rangeSum(H.str2array3("[1,2,3,4]"), 4, 1, 5));
		// 1, 1,2 1,2,3 1,2,3,4
		// 1 1,1,2 1,1,2,1,2,3 1,1,2,1,2,3,1,2,3,4
		// 1 1,1,2 1,1,1,2,2,3 1,1,1,1,2,2,2,3,3,4
		// 1,2,3,4
		// 3,4,3,4
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		// 3,
		// 3,4,
		// 3,4,5
		// 1
		// 1,2
		// 1,2,3
		// 1,2,3,4
		// 1,2,3,4,5
		int prefix[] = new int[arr.length];
		prefix[0] = arr[0];
		for (int i = 1; i < arr.length; i++) {
			prefix[i] = prefix[i - 1] + arr[i];
		}

		int[] pp = new int[arr.length];
		pp[0] = prefix[0];
		for (int i = 1; i < arr.length; i++) {
			pp[i] = pp[i - 1] + prefix[i];
		}
		H.print(arr);
		H.print(prefix);
		H.print(pp);
		int ans = pp[5] - pp[2] - prefix[2] * (5 - 2);
		System.out.println(ans);
//		prefixPrefixSums[j]−prefixPrefixSums[i]−prefixSums[i]×(j−i)
	}

}
